package pl.koszolko.documentclustering.cluster;

import java.io.IOException;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.snowball.SnowballAnalyzer;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.util.Version;

public class VectorCreator {
	public static final String STEMMER = "English";

	public static TextVector createVectorRepresentation(String text) {
		TextVector vectorText = new TextVector();
		Analyzer analyzer = new SnowballAnalyzer(Version.LUCENE_30, STEMMER, StopAnalyzer.ENGLISH_STOP_WORDS_SET);
		StringReader stringReader = new StringReader(text);
		TokenStream tokenStream = analyzer.tokenStream("", stringReader);
		TermAttribute term = tokenStream.getAttribute(TermAttribute.class);

		try {
			while (tokenStream.incrementToken()) {
				vectorText.addWord(term.term());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return vectorText;
	}
}
